import 'dart:async';

import 'package:common_utils/common_utils.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hf/data/user_data.dart';
import 'package:flutter_hf/models/recommend.dart';
import 'package:flutter_hf/widgets/keepalive_wrapper.dart';
import 'package:provider/provider.dart';

import '../provider/home_provider.dart';
import '../router/navigator_util.dart';
import '../router/unit_router.dart';
import '../widgets/common_widget.dart';
import '../widgets/home_common_widget.dart';

class RecommendPage extends StatefulWidget {
  const RecommendPage({Key? key}) : super(key: key);

  @override
  _RecommendPageState createState() => _RecommendPageState();
}

class _RecommendPageState extends State<RecommendPage> with AutomaticKeepAliveClientMixin {
  late EasyRefreshController _controller;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController(
      controlFinishRefresh: true,
      controlFinishLoad: true,
    );
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<HomeProvider>(builder: (
      BuildContext context,
      HomeProvider value,
      Widget? child,
    ) {
      return EasyRefresh(
        header: CommonWidget.classicHeader(),
        footer: CommonWidget.classicFooter(),
        onRefresh: () async {
          if (!mounted) {
            return;
          }

          value.refreshRecommends();
          _controller.finishRefresh();
          _controller.resetFooter();
        },
        onLoad: () async {
          if (!mounted) {
            return;
          }
          value.loadMoreRecommends();
          _controller.finishLoad(IndicatorResult.success);
        },
        child: ListView.builder(
            shrinkWrap: true,
            itemCount: value.recommends.length,
            padding: EdgeInsets.zero,
            itemBuilder: (context, index) {
              final Recommend _re = value.recommends[index];
              return KeepAliveWrapper(
                  child: HomeCommonWidget(
                userId: _re.userId,
                title: _re.nickName,
                url: _re.tempHeadUrl,
                glamour: _re.glamour,
                signature: _re.signature,
                videoCallPrice: _re.videoCallPrice,
                onLineState: _re.onLineState ?? '0',
                onStatusTap: (int? userId) {
                  NavigatorUtil.pushPage(context, UnitRouter.login).then((value) {
                    debugPrint('---😆😆登录返回,value:$value😆😆---');
                    if (ObjectUtil.isNotEmpty(value)) {}
                  });
                  return;
                  if (!UserData().isLogin) {
                    NavigatorUtil.pushPage(context, UnitRouter.login).then((value) {
                      debugPrint('---😆😆登录返回,value:$value😆😆---');
                      if (ObjectUtil.isNotEmpty(value)) {}
                    });
                  }
                },
              ));
            }),
      );
    });
  }

  _loadData() {
    scheduleMicrotask(() {
      /// banner
      Provider.of<HomeProvider>(context, listen: false).loadRecommends();
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
